<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg1{
            background-color: red;
        }
        .bg2{
            background-color: gray;
        }
        .bg3{
            background-color: blueviolet;
        }
        .base{
            width: 300px;
            height:100px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <div class="base">

    </div>
    <script>

        /***
         * 如何操作元素的class属性: className 
         * 
         */
        var bgs = ['bg1','bg2','bg3'];
        var oDiv = document.querySelector('div');
        console.dir(oDiv);
        oDiv.onclick = function(){
            // 计算0-2随机数
            var index = Math.floor(Math.random() * 3);
            var bgClass = bgs[index];
            oDiv.className = "base " + bgClass;

        }
    </script>
</body>
</html>